package com.platform.server.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.server.util.StringTools;

@Controller
public class BackupsAndRecoveryController {
	
	
	/**
	 * 备份
	 * @return
	 */
	@RequestMapping("/backups")
	@ResponseBody
	public Object backups(HttpServletRequest request,String name){
		String[]   cmd = {"mysqldump -h39.108.190.197 -P3306 -uroot -pJnom27LVp$%bM8zx huatianxia "+name+" > /home/backups/"+name+".sql"};
        System.out.println("cmd[0] = " + cmd[0]);
       try {
           Process process = Runtime.getRuntime().exec(
                    new String[]{
                       "sh",
                       "-c",
                       cmd[0]});
           BufferedReader inputBufferedReader = new BufferedReader(
                   new InputStreamReader(process.getInputStream()));
           String line = null; 
           while ((line = inputBufferedReader.readLine()) != null) {
               System.out.println(line);
           }
           try {
               process.waitFor();
           } catch (InterruptedException e) {
               e.printStackTrace();
               return null;
           }

       } catch (IOException e) {
           e.printStackTrace();
           return null;
       }
       return "OK";
	
	}
	/**
	 * 恢复
	 * @param request
	 * @param name
	 * @return
	 */
	@RequestMapping("/recovery")
	@ResponseBody
	public String recovery(HttpServletRequest request,String name){
		String[]   cmd = {"cat /home/backups/"+name+".sql| mysql -uroot -pJnom27LVp$%bM8zx --default-character-set=gbk -h39.108.190.197 huatianxia"};
        System.out.println("cmd[0] = " + cmd[0]);
       try {
           Process process = Runtime.getRuntime().exec(
                    new String[]{
                       "sh",
                       "-c",
                       cmd[0]});
           BufferedReader inputBufferedReader = new BufferedReader(
                   new InputStreamReader(process.getInputStream()));
           String line = null;
           while ((line = inputBufferedReader.readLine()) != null) {
               System.out.println(line);
           }
           try {
               process.waitFor();
           } catch (InterruptedException e) {
               e.printStackTrace();
               return null;
           }

       } catch (IOException e) {
           e.printStackTrace();
           return null;
       }
       return "OK";
	
	}

    /**
	 * 备份所有
	 * @return
	 */
	@RequestMapping("/backupsAll")
	@ResponseBody
	public Object backupsAll(HttpServletRequest request,String name){
		String[]   cmd = {"mysqldump -h39.108.190.197 -P3306 -uroot -pJnom27LVp$%bM8zx huatianxia > /home/backups/huatianxia.sql"};
        System.out.println("cmd[0] = " + cmd[0]);
       try {
           Process process = Runtime.getRuntime().exec(
                    new String[]{
                       "sh",
                       "-c",
                       cmd[0]});
           BufferedReader inputBufferedReader = new BufferedReader(
                   new InputStreamReader(process.getInputStream()));
           String line = null; 
           while ((line = inputBufferedReader.readLine()) != null) {
               System.out.println(line);
           }
           try {
               process.waitFor();
           } catch (InterruptedException e) {
               e.printStackTrace();
               return null;
           }

       } catch (IOException e) {
           e.printStackTrace();
           return null;
       }
       return "OK";
	
	}
	/**
	 * 恢复
	 * @param request
	 * @param name
	 * @return
	 */
	@RequestMapping("/recoveryAll")
	@ResponseBody
	public String recoveryAll(HttpServletRequest request,String name){
		String[]   cmd = {"cat /home/backups/huatianxia.sql | mysql -uroot -pJnom27LVp$%bM8zx --default-character-set=gbk -h39.108.190.197 huatianxia"};
        System.out.println("cmd[0] = " + cmd[0]);
       try {
           Process process = Runtime.getRuntime().exec(
                    new String[]{
                       "sh",
                       "-c",
                       cmd[0]});
           BufferedReader inputBufferedReader = new BufferedReader(
                   new InputStreamReader(process.getInputStream()));
           String line = null;
           while ((line = inputBufferedReader.readLine()) != null) {
               System.out.println(line);
           }
           try {
               process.waitFor();
           } catch (InterruptedException e) {
               e.printStackTrace();
               return null;
           }

       } catch (IOException e) {
           e.printStackTrace();
           return null;
       }
       return "OK";
	
	}
}
